package sau.diyizu.media.common;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import sau.diyizu.media.R;

/**
 @author JYD
 @version 1.0
 @since 2020/12/16 23:42 */
public class RenameDialog extends Dialog {
    public static final String TAG = "RenameDialog";
    private final EditText content_ev;
    private final TextView hint_tv;
    private final Button confirm_btn;

    private String name;
    /** extension name */
    private String exName;
    /** no have extension name */
    private String noExName;

    private OnConfirmListener onConfirmListener = OnConfirmListener.DEFAULT_NULL_INSTANCE;

    public void setOnConfirmListener(OnConfirmListener onConfirmListener) {
        if (onConfirmListener == null && this.onConfirmListener != OnConfirmListener.DEFAULT_NULL_INSTANCE) {
            this.onConfirmListener = OnConfirmListener.DEFAULT_NULL_INSTANCE;
            return;
        }
        this.onConfirmListener = onConfirmListener;
    }

    public RenameDialog(@NonNull Context context) {
        super(context, R.style.BaseDialogTheme);
        setContentView(R.layout.dialog_rename);
        content_ev = findViewById(R.id.content_ev);
        hint_tv = findViewById(R.id.hint_tv);
        confirm_btn = findViewById(R.id.confirm_btn);

        content_ev.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) { }

            @Override
            public void afterTextChanged(Editable s) {
                boolean enabled = !s.toString().equals(noExName);
                confirm_btn.setEnabled(enabled);
                hint_tv.setVisibility(enabled ? View.GONE : View.VISIBLE);
            }
        });

        confirm_btn.setOnClickListener(v -> {
            String newName = content_ev.getText().toString();
            if (this.exName != null) newName += exName;
            onConfirmListener.onConfirm(newName);
        });
    }

    public void show(String name, String... exNames) {
        if (name == null || name.length() <= 0) return;
        super.show();
        this.name = name;
        int lastIndex = name.lastIndexOf('.');
        if (lastIndex != -1) {
            this.exName = name.substring(lastIndex);
            this.noExName = name.substring(0, lastIndex);

            boolean hasMatch = false;
            for (String exName : exNames) {
                if (this.exName.equals(exName)) {
                    hasMatch = true;
                    break;
                }
            }
            if (!hasMatch) {
                // 文件后缀名和指定后缀名不匹配
                Log.w(TAG, "show: The file extension name does not match the specified extension name");
                this.exName = null;
                this.noExName = null;
            }
        }
        content_ev.setText(noExName != null ? noExName : name);
    }

    public interface OnConfirmListener {
        OnConfirmListener DEFAULT_NULL_INSTANCE = newName -> {};

        /** 判断是否为默认空实现 */
        default boolean isNull() { return this == DEFAULT_NULL_INSTANCE; }

        void onConfirm(String newName);
    }
}
